package com.hetang.maijiayun.adaptertools.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.http.client.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hetang.maijiayun.adaptertools.b2cinterface.weidian.client.WeidianClient;
import com.hetang.maijiayun.adaptertools.b2cinterface.weidian.domain.Order;
import com.hetang.maijiayun.adaptertools.b2cinterface.weidian.request.OrderListGetRequest;
import com.hetang.maijiayun.adaptertools.b2cinterface.weidian.response.OrderListGetResponse;
import com.hetang.maijiayun.adaptertools.controller.vo.request.DetectorRequest;
import com.hetang.maijiayun.adaptertools.utils.JsonTools;

public class WeidianAdapter extends AdapterAbstract {
	/**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(WeidianAdapter.class);

    @Override
    public Integer request(Map<String, String> authorizesMap, DetectorRequest request) throws Exception {
        return null;
    }

    @Override
    public List<String> requestOrders(Map<String, String> authorizesMap, DetectorRequest request) throws Exception {
        if (MapUtils.isEmpty(authorizesMap) || !authorizesMap.containsKey("url") || !authorizesMap.containsKey("appKey") || !authorizesMap.containsKey("secret") || !authorizesMap.containsKey("sessionKey")) {
            logger.error("该微店平台店铺没有授权信息或授权信息错误！！");
            return null;
        }
        WeidianClient client = new WeidianClient(authorizesMap.get("appKey"), authorizesMap.get("secret"), authorizesMap.get("sessionKey"));
        OrderListGetRequest req = new OrderListGetRequest();
        req.setPage_num("1");
        req.setPage_size("50");
        req.setUpdate_start(DateUtils.formatDate(request.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
        req.setUpdate_end(DateUtils.formatDate(request.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
        logger.info("weidian get order interface, request time is : {}, request param is : {}", System.currentTimeMillis(), JsonTools.createJsonString(req));
        OrderListGetResponse rsp = client.execute(req);
        logger.info("weidian get order interface, response time is : {}, response : {}", System.currentTimeMillis(), JsonTools.createJsonString(rsp));
        if (rsp == null || !"0".equals(rsp.getStatus_code())) {
            return null;
        }
        Long page = 0L;
        Long mod = rsp.getTotal_num()%50L;
        page = rsp.getTotal_num()/50L;
        if (0 != mod) {
            page += 1;
        }
        List<String> result = new ArrayList<String>();
        while (page > 0) {
            req.setPage_num(page.toString());
            rsp = client.execute(req);
            if (!"0".equals(rsp.getStatus_code())) {
                logger.error("weidian get order interface error, response time is : {}, response : {}", System.currentTimeMillis(), JsonTools.createJsonString(rsp));
                continue;
            }
            if (CollectionUtils.isEmpty(rsp.getOrders())) {
                continue;
            }
            for (Order order : rsp.getOrders()) {
                result.add(order.getOrder_id() + "");
            }
            page -- ;
        }
        return result;
    }
}
